package com.bj58.data.emailreports.controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.bj58.data.emailreports.model.BWRAllLineInc;
import com.bj58.data.emailreports.model.BWRAllOne;
import com.bj58.data.emailreports.model.BWRAllOneJL;
import com.bj58.data.emailreports.model.BWRJzFluxFunnel;
import com.bj58.data.emailreports.model.BWRLineAccount;
import com.bj58.data.emailreports.model.BWRLineCityCate;
import com.bj58.data.emailreports.model.BWRLineCusTran;
import com.bj58.data.emailreports.model.BWRLineFlux;
import com.bj58.data.emailreports.model.BWRLineJTChange;
import com.bj58.data.emailreports.model.BWRLineSPIncome;
import com.bj58.data.emailreports.model.BWRNoteEffectData;
import com.bj58.data.emailreports.model.WRCusFunnelVo;
import com.bj58.data.emailreports.model.BWRBalanceCusLevel;
import com.bj58.data.emailreports.service.BusiWeekReportService;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.EmailReportUtil;
import com.bj58.data.emailreports.utils.MailUtil;
import com.bj58.data.emailreports.utils.PatternUtil;
import com.bj58.data.emailreports.utils.ResourcesUtil;
import com.bj58.data.emailreports.utils.SomePublicUtil;
import com.bj58.data.emailreports.utils.SortUtil;
import com.bj58.data.emailreports.utils.SubmitSms;
import com.bj58.data.emailreports.utils.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.PathUtil;

/**商业数据分析周报
 * @author huangliang
 *
 */
public class BusiWeekReportController {
	private BusiWeekReportService busiWeekReportService= ServiceLocator.get(BusiWeekReportService.class);
	private EmailReportUtil eru = new EmailReportUtil();
	private SortUtil sortUtil = new SortUtil();
	private SomePublicUtil spu = new SomePublicUtil();
	
	
	/**第一部分  总体 - 进度/ 总体 - 简历 进度  / 收入趋势  /会员收入趋势/流量趋势/页面类型流量趋势/精准各平台流量趋势/在线产品收入趋势-贴均效果
	 * @param searchDate
	 * @return
	 */
	public String weekReportPartOne(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiWeekReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			String yesterday = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");//查询日期（昨）
			String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");//查询日期（上周）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(yesterday);dateList.add(lastWeek);
			
			List<String> dateList_search = new ArrayList<String>();
			dateList_search.add(today);dateList_search.add(lastWeek);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastDate", yesterday);
			dateMap.put("lastWeek",lastWeek);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("yesterday", yesterday.replaceAll("-", "."));
			conMap.put("lastWeek", lastWeek.replaceAll("-", "."));
			
			
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
			List<String> percentList = new ArrayList<String>();//百分比指标集
			
			/**注意 ：
			 * 周报计算的是本周和上周的平均值 并进行周比
			 * yesterday 不用  但为了模版计算占位仍保留  在页面显示时处理不显示日环比
			 * 如 模板中 #elseif($velocityCount % 3 == 0)  就表示去除日环比数据
			 */
		
	 /*
			//总体 - 进度
			List<BWRAllOne> BWRAllOneList = busiWeekReportService.getBWRAllOneList(today);
			if(BWRAllOneList.get(0).getAllDone() == 0 || BWRAllOneList.get(0).getAllKpi() == 0){
				System.out.println(" BWRAllOneList check is null ");
				return "";
			}		
			BWRAllOneList = sortUtil.sortListByFileNameVo(BWRAllOneList, BWRAllOne.class, "allDone", 2);
			conMap.put("BWRAllOneList", BWRAllOneList);

*/
			//总体 - 简历 进度
			Map<String,BWRAllOneJL> BWRAllOneJLMap = busiWeekReportService.getBWRAllOneJLList(dateList);
			conMap.put("BWRAllOneJLMap", BWRAllOneJLMap);
			conMap.put("jlToday", BWRAllOneJLMap.get(today));
			conMap.put("jlLastWeek", BWRAllOneJLMap.get(lastWeek));
	
				
			//分业务-收入趋势					
			showCols.clear();
			showCols.add("cost");showCols.add("cash");showCols.add("cusCount");showCols.add("ARPU");
			showCols.add("jzCost");showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("jzARPU");
			showCols.add("zdCost");showCols.add("zdCash");showCols.add("zdCusCount");showCols.add("zdARPU");
			showCols.add("znCost");showCols.add("znCash");showCols.add("znCusCount");showCols.add("znARPU");
			
			List<BWRAllLineInc> BWRAllLineIncList = busiWeekReportService.getBWRAllLineIncList(dateList_search,"all");	
			List<String> BWRAllLineIncDatas = eru.conformDataTypeFour(BWRAllLineIncList, BWRAllLineInc.class, "busiLine", showCols, "statDate", null, dateList);
			BWRAllLineIncDatas = SortUtil.sortListByFileName(BWRAllLineIncDatas, 1, 2);
			conMap.put("BWRAllLineIncDatas", BWRAllLineIncDatas);
			
			
			//分业务-会员收入趋势					
			showCols.clear();
			showCols.add("cost");showCols.add("cash");showCols.add("cusCount");showCols.add("ARPU");
			showCols.add("jzCost");showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("jzARPU");
			showCols.add("zdCost");showCols.add("zdCash");showCols.add("zdCusCount");showCols.add("zdARPU");
			showCols.add("znCost");showCols.add("znCash");showCols.add("znCusCount");showCols.add("znARPU");
			
			List<BWRAllLineInc> BWRAllLineVipIncList = busiWeekReportService.getBWRAllLineIncList(dateList_search,"vip");	
			List<String> BWRAllLineVipIncDatas = eru.conformDataTypeFour(BWRAllLineVipIncList, BWRAllLineInc.class, "busiLine", showCols, "statDate", null, dateList);
			BWRAllLineVipIncDatas = SortUtil.sortListByFileName(BWRAllLineVipIncDatas, 1, 2);
			conMap.put("BWRAllLineVipIncDatas", BWRAllLineVipIncDatas);
	
				
			//分业务-流量趋势
			showCols.clear();
			showCols.add("pv");showCols.add("adpv");showCols.add("adimp");
			showCols.add("click");showCols.add("PVR");showCols.add("ASN");
			showCols.add("CPM");showCols.add("ACP");
			showCols.add("CTR2");
			percentList.clear();
			percentList.add("PVR");percentList.add("CTR2");
			
			Map<String,List<String>> BWRLineFluxRS = new HashMap<String, List<String>>();			
			Map<String,List<BWRLineFlux>> BWRLineFluxMap= busiWeekReportService.getBWRAllLineFluxList(dateList_search);
			for(Iterator<String> it = BWRLineFluxMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineFlux> tempList = BWRLineFluxMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineFlux.class, "incomeType", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRLineFluxRS.put(busiLine, dataList);
			}
			conMap.put("BWRLineFluxRS", BWRLineFluxRS);
					
			
			//分业务-页面类型流量趋势
			showCols.clear();
			showCols.add("pv");showCols.add("adpv");showCols.add("adimp");
			showCols.add("click");showCols.add("PVR");showCols.add("ASN");
			showCols.add("CPM");showCols.add("ACP");
			showCols.add("CTR2");
			percentList.clear();
			percentList.add("PVR");percentList.add("CTR2");
			
			Map<String,List<String>> BWRLinePageFluxRS = new HashMap<String, List<String>>();			
			Map<String,List<BWRLineFlux>> BWRLinePageFluxMap= busiWeekReportService.getBWRLinePageFluxList(dateList_search);
			for(Iterator<String> it = BWRLinePageFluxMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineFlux> tempList = BWRLinePageFluxMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineFlux.class, "incomeType", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRLinePageFluxRS.put(busiLine, dataList);
			}
			conMap.put("BWRLinePageFluxRS", BWRLinePageFluxRS);
					
			showCols.clear();
			showCols.add("pv");showCols.add("adpv");showCols.add("adimp");
			showCols.add("click");showCols.add("PVR");showCols.add("ASN");
			showCols.add("CPM");showCols.add("ACP");
			showCols.add("CTR2");
			percentList.clear();
			percentList.add("PVR");percentList.add("CTR2");
			//分业务-精准各平台流量趋势
			Map<String,List<String>> BWRLineJingzhunFluxRS = new HashMap<String, List<String>>();			
			Map<String,List<BWRLineFlux>> BWRLineJingzhunFluxMap= busiWeekReportService.getBWRLineJingzhunFluxList(dateList_search);
			for(Iterator<String> it = BWRLineJingzhunFluxMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineFlux> tempList = BWRLineJingzhunFluxMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineFlux.class, "incomeType", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRLineJingzhunFluxRS.put(busiLine, dataList);
			}
			conMap.put("BWRLineJingzhunFluxRS", BWRLineJingzhunFluxRS);

			
		    //在线产品收入趋势-贴均效果
			showCols.clear();
			showCols.add("jzCost");showCols.add("jzCusCount");showCols.add("jzCusAvgInfo");showCols.add("ACP");showCols.add("jzInfoAvgEffect");
			showCols.add("zdCost");showCols.add("zdCusCount");showCols.add("zdCusAvgInfo");showCols.add("zdTime");
			showCols.add("zdHourPrice");showCols.add("zdInfoAvgEffect");
	
			Map<String,List<String>> BWRNoteEffectRS = new HashMap<String, List<String>>();			
			Map<String,List<BWRNoteEffectData>> BWRNoteEffectMap= busiWeekReportService.getBWRNoteEffectList(dateList_search);
			for(Iterator<String> it = BWRNoteEffectMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRNoteEffectData> tempList = BWRNoteEffectMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRNoteEffectData.class, "CusType", showCols, "statDate", null, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRNoteEffectRS.put(busiLine, dataList);
			}
			conMap.put("BWRNoteEffectRS", BWRNoteEffectRS);
			
			
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	
	/**第二部分 /精准置顶参数变化 /精准城市流量漏斗/精准类别流量漏斗/余额客户结构/余额客户结构 -客户分层/消耗周期
	 * @param searchDate
	 * @return
	 */
	public String weekReportPartTwo(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiWeekReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			String yesterday = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");//查询日期（昨）
			String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");//查询日期（上周）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(yesterday);dateList.add(lastWeek);
			
			List<String> dateList_search = new ArrayList<String>();
			dateList_search.add(today);dateList_search.add(lastWeek);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastDate", yesterday);
			dateMap.put("lastWeek",lastWeek);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("yesterday", yesterday.replaceAll("-", "."));
			conMap.put("lastWeek", lastWeek.replaceAll("-", "."));
					
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
			List<String> percentList = new ArrayList<String>();//百分比指标集
			
			/**注意 ：
			 * 周报计算的是本周和上周的平均值 并进行周比
			 * yesterday 不用  但为了模版计算占位仍保留  在页面显示时处理不显示日环比
			 * 如 模板中 #elseif($velocityCount % 3 == 0)  就表示去除日环比数据
			 */
			
		
			// 分业务线 - 精准置顶参数变化					
			percentList.clear();
			percentList.add("jzShowCusRate");percentList.add("zdShowCusRate");
			
			List<String> JTChangedicsList = spu.getECDicByAnnotation(BWRLineJTChange.class);
			Map<String,List<String>> BWRLineJTChangeRS = new HashMap<String, List<String>>();			
			Map<String,List<BWRLineJTChange>> BWRLineJTChangeMap= busiWeekReportService.getBWRLineJTChangeMap(dateList_search);
			for(Iterator<String> it = BWRLineJTChangeMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineJTChange> tempList = BWRLineJTChangeMap.get(busiLine);			
				List<String> dataList = eru.conformDataTypeOne(tempList, BWRLineJTChange.class, percentList, dateMap, "statDate", JTChangedicsList);
				BWRLineJTChangeRS.put(busiLine, dataList);
			}
			conMap.put("BWRLineJTChangeRS", BWRLineJTChangeRS);
					
			
			//分业务- 精准城市流量漏斗
			showCols.clear();
			showCols.add("cost");showCols.add("pv");showCols.add("PVR");
			showCols.add("adimp");showCols.add("ASN");showCols.add("click");
			showCols.add("CTR1");showCols.add("CTR2");showCols.add("CTR3");
			percentList.clear();
			percentList.add("PVR");percentList.add("CTR1");
			percentList.add("CTR2");percentList.add("CTR3");
			
			Map<String,List<String>> BWRJzFluxFunnelCityRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRJzFluxFunnel>> BWRJzFluxFunnelCityMap= busiWeekReportService.getBWRJzFluxFunnelCityMap(dateList_search);
			for(Iterator<String> it = BWRJzFluxFunnelCityMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRJzFluxFunnel> tempList = BWRJzFluxFunnelCityMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRJzFluxFunnel.class, "type", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRJzFluxFunnelCityRS.put(busiLine, dataList);
			}
			conMap.put("BWRJzFluxFunnelCityRS", BWRJzFluxFunnelCityRS);
			
			
			//分业务- 精准类别流量漏斗
			showCols.clear();
			showCols.add("cost");showCols.add("pv");showCols.add("PVR");
			showCols.add("adimp");showCols.add("ASN");showCols.add("click");
			showCols.add("CTR1");showCols.add("CTR2");showCols.add("CTR3");		
			percentList.clear();
			percentList.add("PVR");percentList.add("CTR1");
			percentList.add("CTR2");percentList.add("CTR3");
			
			Map<String,List<String>> BWRJzFluxFunnelCateRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRJzFluxFunnel>> BWRJzFluxFunnelCateMap= busiWeekReportService.getBWRJzFluxFunnelCateMap(dateList_search);
			for(Iterator<String> it = BWRJzFluxFunnelCateMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRJzFluxFunnel> tempList = BWRJzFluxFunnelCateMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRJzFluxFunnel.class, "type", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRJzFluxFunnelCateRS.put(busiLine, dataList);
			}
			conMap.put("BWRJzFluxFunnelCateRS", BWRJzFluxFunnelCateRS);		
		
			
			//业务线 - 余额客户结构   和旧的保持一致...
			showCols.clear();
			showCols.add("allCus");showCols.add("left0");showCols.add("left50");
			showCols.add("cashLeft0");showCols.add("cashLeft50");
			showCols.add("preCus");showCols.add("preRate");
			showCols.add("topCus");showCols.add("topRate");
			showCols.add("idCus");showCols.add("idRate");		
			percentList.clear();
			percentList.add("preRate");
			percentList.add("topRate");
			percentList.add("idRate");
			
			HashMap<String,List<String>>  cusFunnelMap = new HashMap<String, List<String>>();
            String cusFunnelChannel= "招聘,黄页,二手车,房产,二手创新,招商加盟";	
			//String cusFunnelChannel= "二手创新";
			
			for (String channel : cusFunnelChannel.split(",")) {
				List<WRCusFunnelVo> cusFunnelList = busiWeekReportService.cusBanlanceDatas(dateList_search,channel);
				List<String> cusFunnelDatas = eru.conformDataTypeFour(cusFunnelList, WRCusFunnelVo.class, "type", showCols, "statDate", percentList, dateList);
				cusFunnelDatas = PatternUtil.mailWarnCheck(cusFunnelDatas, 10);
				cusFunnelDatas = SortUtil.sortListByFileName(cusFunnelDatas, 1, 2);			
				cusFunnelMap.put(channel, cusFunnelDatas);
			}
			conMap.put("cusFunnelMap", cusFunnelMap);
				
								
			//余额客户结构 -- 客户分层
			showCols.clear();
			showCols.add("balance");showCols.add("cashBalance");showCols.add("cost");
			showCols.add("Cus");showCols.add("balanceCus");showCols.add("ARPU");
			showCols.add("frequency_userid");showCols.add("costCyc");	
			
			Map<String,List<String>> BWRBalanceCusLevelRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRBalanceCusLevel>> BWRBalanceCusLevelMap= busiWeekReportService.getBWRBalanceCusLevelMap(dateList_search);
			for(Iterator<String> it = BWRBalanceCusLevelMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRBalanceCusLevel> tempList = BWRBalanceCusLevelMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRBalanceCusLevel.class, "CusType", showCols, "statDate", null, dateList);
				
				String[] cate={"总体","会员","会员活跃","会员大客户","会员小客户","会员非活跃","非会员","非会员活跃","非会员非活跃"};
				List<String> rsList=new ArrayList<String>();
				
				for(String c:cate)
				{
					for(String s:dataList)
					{
						if(s.split(";")[0].contains(c))
						{						
							s=s.replaceAll(";", "@");												
							String t="";
							if(c.equals("总体") || c.equals("会员") ||c.equals("非会员") )
							{
								int n=s.indexOf("@");	
								t="<td class='td2'><b>"+s.substring(0, n)+"</b></td>"+s.substring(n);
							}
							if(c.equals("会员活跃") || c.equals("会员非活跃") ||c.equals("非会员活跃") || c.equals("非会员非活跃") )
							{
								int n=s.indexOf("@");	
								t="<td class='td2'>&nbsp;&nbsp;&nbsp;&nbsp;"+s.substring(0, n)+"</td>"+s.substring(n);
							}
							else if(c.equals("会员大客户") || c.equals("会员小客户"))
							{
								int n=s.indexOf("@");	
								t="<td class='td2'><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+s.substring(0, n)+"</i></td>"+s.substring(n);
							}		
							
							rsList.add(t);
							break;
						}
					}
				}
						
				BWRBalanceCusLevelRS.put(busiLine, rsList);
			}
			conMap.put("BWRBalanceCusLevelRS", BWRBalanceCusLevelRS);
			
						
			//消耗周期 -- 和旧的保持一致即可
			HashMap<String,List<String>> consumCycleMap = new HashMap<String, List<String>>();
			HashMap<String,String> consumCycleCate2Map = new HashMap<String,String>();
			String channelsConsumCycle = "黄页,招聘,二手创新,招商加盟,二手车,房产";
	//		String channelsConsumCycle = "招商加盟";
			
			for (String channel : channelsConsumCycle.split(",")) {
				String cate1 = "";
				String cate2 = "";
				
				if("黄页".equals(channel)){
					cate1 = " source_cate1_name in ('农林牧副渔','招商加盟','休闲娱乐','医疗健康','商务服务','婚庆摄影','家政服务','教育培训','汽车服务','旅游酒店','装修建材','餐饮美食','批发采购','丽人','公共服务') and source_cate2_name <> '二手回收' ";
					cate2 = "搬家,保洁清洗,保姆/月嫂,工商注册,家电维修,投资担保,租车,%家装服务%";
				}else if("招聘".equals(channel)){
					cate1 = " source_cate1_name in ('全职招聘','兼职招聘') ";
					cate2 = "";
				}else if("二手创新".equals(channel)){
					cate1 = "  (source_cate1_name ='宠物' or source_cate2_name in ('二手回收','办公用品/设备','艺术品/收藏品','手机号码','二手设备','通讯业务','成人用品','校园二手','二手求购') ) ";
					cate2 = "宠物猫,宠物狗,办公用品/设备,艺术品/收藏品,二手设备,二手求购,花鸟鱼虫,二手回收";
				}else if("招商加盟".equals(channel)){
					cate1 = " source_cate1_name='招商加盟' ";
					cate2 = "建材加盟,服装鞋包,特色加盟,美容保健加盟,餐饮加盟";
				}else if("房产".equals(channel)){
					cate1 = " source_cate1_name='房产信息' ";
					cate2 = "%二手房%,%租房%,%合租房%,%写字楼%,%商铺租售/生意转让%,%厂房/仓库/土地/车位%";
				}else if("二手车".equals(channel)){
					cate1 = " source_cate2_name = '二手车' ";
					cate2 = "";
				}
				List<String> cusFunnelList = busiWeekReportService.consumCycleData(today, lastWeek, channel, cate1, cate2);
				consumCycleMap.put(channel, cusFunnelList);
				cate2 = cate2.replace("%", "");
				consumCycleCate2Map.put(channel, cate2);
			}
			conMap.put("consumCycleMap", consumCycleMap);
			conMap.put("consumCycleCate2Map", consumCycleCate2Map);			
		
		
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	
	/**第三部分  /城市监控/类别监控/城市类别问题定位/重点城市类别监控
	 * @param searchDate
	 * @return
	 */
	public String weekReportPartThree(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiWeekReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			String yesterday = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");//查询日期（昨）
			String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");//查询日期（上周）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(yesterday);dateList.add(lastWeek);
			
			List<String> dateList_search = new ArrayList<String>();
			dateList_search.add(today);dateList_search.add(lastWeek);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastDate", yesterday);
			dateMap.put("lastWeek",lastWeek);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("yesterday", yesterday.replaceAll("-", "."));
			conMap.put("lastWeek", lastWeek.replaceAll("-", "."));
			
			
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
			List<String> percentList = new ArrayList<String>();//百分比指标集
			
			/**注意 ：
			 * 周报计算的是本周和上周的平均值 并进行周比
			 * yesterday 不用  但为了模版计算占位仍保留  在页面显示时处理不显示日环比
			 * 如 模板中 #elseif($velocityCount % 3 == 0)  就表示去除日环比数据
			 */
			
			
			//业务线 - 城市监控
			showCols.clear();
			showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("zdCash");
			showCols.add("zdCusCount");showCols.add("znCash");showCols.add("znCusCount");
			showCols.add("allPv");
			showCols.add("fluxTranRate");showCols.add("cusAdequacy");
			percentList.clear();
			percentList.add("fluxTranRate");percentList.add("cusAdequacy");
			
			Map<String,List<String>> BWRCitysRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRLineCityCate>> BWRCitysMap= busiWeekReportService.cityDatas(dateList_search);
			for(Iterator<String> it = BWRCitysMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineCityCate> tempList = BWRCitysMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineCityCate.class, "type", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRCitysRS.put(busiLine, dataList);
			}		
			conMap.put("BWRCitysRS", BWRCitysRS);
			
			
			//业务线 - 城市类别问题定位
			Map<String,List<String>> BWRCitysProbJZMap= busiWeekReportService.cityProbDatas(dateList,"jingzhun");		
			conMap.put("BWRCitysProbJZMap", BWRCitysProbJZMap);
			
			Map<String,List<String>> BWRCitysProbZDMap= busiWeekReportService.cityProbDatas(dateList,"zhiding");		
			conMap.put("BWRCitysProbZDMap", BWRCitysProbZDMap);
					
				
			//业务线 - 类别监控
			showCols.clear();
			showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("zdCash");
			showCols.add("zdCusCount");showCols.add("znCash");showCols.add("znCusCount");
			showCols.add("allPv");
			showCols.add("fluxTranRate");showCols.add("cusAdequacy");			
			percentList.clear();
			percentList.add("fluxTranRate");percentList.add("cusAdequacy");
			
			Map<String,List<String>> BWRCatesRS = new HashMap<String, List<String>>();			
			Map<String,List<BWRLineCityCate>> BWRCatesMap= busiWeekReportService.cateDatas(dateList_search);
			for(Iterator<String> it = BWRCatesMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineCityCate> tempList = BWRCatesMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineCityCate.class, "type", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRCatesRS.put(busiLine, dataList);
			}
			conMap.put("BWRCatesRS", BWRCatesRS);
			
			
			//业务线 - 重点城市类别监控
			showCols.clear();showCols.add("allCash");
			showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("zdCash");
			showCols.add("zdCusCount");showCols.add("znCash");showCols.add("znCusCount");
			showCols.add("allPv");
			showCols.add("fluxTranRate");showCols.add("cusAdequacy");
			percentList.clear();
			percentList.add("fluxTranRate");percentList.add("cusAdequacy");
			
			Map<String,List<String>> BWRCityCateRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRLineCityCate>> BWRCityCateMap= busiWeekReportService.cityCateDatas(dateList_search);
			for(Iterator<String> it = BWRCityCateMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineCityCate> tempList = BWRCityCateMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineCityCate.class, "type", showCols, "statDate", percentList, dateList);
				
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				for(int i=0;i<dataList.size();i++)
				{
					String s =dataList.get(i);
					String cc=s.split(";")[0];
					s=cc.split("_")[0]+";"+cc.split("_")[1]+";"+s.substring(s.indexOf(";")+1, s.length());
					
					dataList.set(i,s);

				}
				BWRCityCateRS.put(busiLine, dataList);
			}		
			conMap.put("BWRCityCateRS", BWRCityCateRS);
		
			
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	
	/**第四部分  /分平台收入变化/分来源收入变化/新增流失客户/余额指标
	 * @param searchDate
	 * @return
	 */
	public String weekReportPartFour(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiWeekReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			String yesterday = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");//查询日期（昨）
			String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");//查询日期（上周）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(yesterday);dateList.add(lastWeek);
			
			List<String> dateList_search = new ArrayList<String>();
			dateList_search.add(today);dateList_search.add(lastWeek);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastDate", yesterday);
			dateMap.put("lastWeek",lastWeek);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("yesterday", yesterday.replaceAll("-", "."));
			conMap.put("lastWeek", lastWeek.replaceAll("-", "."));
					
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
			List<String> percentList = new ArrayList<String>();//百分比指标集
			
			/**注意 ：
			 * 周报计算的是本周和上周的平均值 并进行周比
			 * yesterday 不用  但为了模版计算占位仍保留  在页面显示时处理不显示日环比
			 * 如 模板中 #elseif($velocityCount % 3 == 0)  就表示去除日环比数据
			 */

			
			//业务线 - 分平台收入变化
			showCols.clear();
			showCols.add("jzPv");showCols.add("jzPvZb");showCols.add("jzCash");
			showCols.add("jzCashZb");showCols.add("jzTranRate");
			showCols.add("zdPv");showCols.add("zdPvZb");showCols.add("zdCash");
			showCols.add("zdCashZb");showCols.add("zdTranRate");		
			percentList.clear();
			percentList.add("jzPvZb");percentList. add("jzCashZb");percentList.add("jzTranRate");
			percentList.add("zdPvZb");percentList.add("zdCashZb");percentList.add("zdTranRate");
			
			Map<String,List<String>> BWRLinePlatformRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRLineSPIncome>> BWRLinePlatformMap= busiWeekReportService.linePlatformIncomeDatas(dateList_search);
			for(Iterator<String> it = BWRLinePlatformMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineSPIncome> tempList = BWRLinePlatformMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineSPIncome.class, "type", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRLinePlatformRS.put(busiLine, dataList);
			}
			conMap.put("BWRLinePlatformRS", BWRLinePlatformRS);
			
	
			//业务线 - 分来源收入变化
			showCols.clear();
			showCols.add("jzPv");showCols.add("jzPvZb");showCols.add("jzCash");
			showCols.add("jzCashZb");showCols.add("jzTranRate");
			showCols.add("zdPv");showCols.add("zdPvZb");showCols.add("zdCash");
			showCols.add("zdCashZb");showCols.add("zdTranRate");		
			percentList.clear();
			percentList.add("jzPvZb");percentList.add("jzCashZb");percentList.add("jzTranRate");
			percentList.add("zdPvZb");percentList.add("zdCashZb");percentList.add("zdTranRate");
			
			Map<String,List<String>> BWRLineSourceRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRLineSPIncome>> BWRLineSourceMap= busiWeekReportService.lineSourceIncomeDatas(dateList_search);
			for(Iterator<String> it = BWRLineSourceMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineSPIncome> tempList = BWRLineSourceMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineSPIncome.class, "type", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRLineSourceRS.put(busiLine, dataList);
			}
			conMap.put("BWRLineSourceRS", BWRLineSourceRS);
		
			
			//业务线 - 新增流失客户
			showCols.clear();
			showCols.add("showCus");showCols.add("costCus");showCols.add("costARPU");
			showCols.add("addCus");showCols.add("addARPU");
			showCols.add("lostCus");showCols.add("lostARPU");showCols.add("toJZ");
			showCols.add("toZD");showCols.add("lostZB");		
			percentList.clear();
			percentList.add("toJZ");percentList.add("toZD");percentList.add("lostZB");
			
			Map<String,List<String>> BWRLineCusTranRS = new HashMap<String, List<String>>();			
			Map<String,List<BWRLineCusTran>> BWRLineCusTranMap= busiWeekReportService.BWRLineCusTranDatas(dateList_search);
			for(Iterator<String> it = BWRLineCusTranMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineCusTran> tempList = BWRLineCusTranMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFour(tempList, BWRLineCusTran.class, "incomeType", showCols, "statDate", percentList, dateList);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BWRLineCusTranRS.put(busiLine, dataList);
			}
			conMap.put("BWRLineCusTranRS", BWRLineCusTranRS);
				
						
			//业务线 - 余额指标
			List<String>  dicsList = spu.getECDicByAnnotation(BWRLineAccount.class);		
			percentList.clear();
			
			Map<String,List<String>> BWRLineAccountRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRLineAccount>> BWRLineAccountMap= busiWeekReportService.BWRLineAccountDatas(dateList_search);
			for(Iterator<String> it = BWRLineAccountMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineAccount> tempList = BWRLineAccountMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeOne(tempList, BWRLineAccount.class, percentList, dateMap, "statDate", dicsList);
				BWRLineAccountRS.put(busiLine, dataList);
			}
			conMap.put("BWRLineAccountRS", BWRLineAccountRS);
			
			
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	
	public static void main(String[] args) {
		try {	
		
			String searchDate="2016-03-17";//周四
			BusiWeekReportController test = new BusiWeekReportController();
			//String content = test.weekReportPartFour(searchDate);
			//String content = test.weekReportPartThree(searchDate);
			//String content = test.weekReportPartTwo(searchDate);
			String content = test.weekReportPartOne(searchDate);
			
			if(!"".equals(content)){
				String text="新商业数据周报测试  "+searchDate;
				MailUtil.javaMailSend(text, content, "guoxue@58.com",null);
			}else{
				//邮件报警
//				content = "新商业周报报错！";
//		    	MailUtil.javaMailSend("email report error", content, "guoxue@58.com",null);
//		    	Map<String,String> telMap=new HashMap<String,String>();
//		    	telMap.put("15726690526","email report error:"+content);
//		    	SubmitSms.submitMsg(telMap);
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
